import Vue from 'vue'
import Vuex from 'vuex'
import request from '@/utils/index.js'

// 1. 导入包
import createPersistedState from 'vuex-persistedstate'
Vue.use(Vuex)

export default new Vuex.Store({
  // 2. 配置为 vuex 的插件
  plugins: [createPersistedState()],
  state: {
    token: '',
    userInfo: {}
  },
  mutations: {
    updateToken(state, newToken) {
      state.token = newToken
    },
    updateUserInfo(state, info) {
      state.userInfo = info
    }
  },
  actions: {
    async initUserInfo(ctx) {
      const { data: res } = await request.get('/my/userinfo')
      if (res.code === 0) {
        ctx.commit('updateUserInfo', res.data)
      }
    }
  },
  modules: {
  }
})
